package com.csdj.web;

import com.alibaba.fastjson.JSONArray;
import com.csdj.entity.Employee;
import com.csdj.service.EmployeeService;
import com.csdj.service.impl.EmployeeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/searchName")
public class SearchNameServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置字符编码
        resp.setContentType("text/html;charset=utf-8");
        //获取请求内容
        String name = req.getParameter("name");
        //调用业务层对象
        EmployeeService employeeService = new EmployeeServiceImpl();
       List<Employee> emps= employeeService.getEmployeeInfoList(name);
        //响应输出结果
        PrintWriter out = resp.getWriter();
        out.write("");
        out.write(JSONArray.toJSONString(emps));
        out.close();
    }
}
